# import json
# import re
#
# import pyttsx3
# from vosk import Model, KaldiRecognizer
# import pyaudio
#
# from utils.logger import log
# from utils.utils import CommonUtils
#
#
# class Audio:
#
#     def __init__(self, model_name='vosk-model-small-cn-0.22', speaker=None):
#         self.model_name = model_name
#         self.model_path = CommonUtils.resource_path([self.model_name])
#         print(self.model_path)
#         self.model = Model(model_path=self.model_path, model_name=self.model_name)
#         # 初始化识别器
#         self.recognizer = KaldiRecognizer(self.model, 16000)
#
#         self.speaker = speaker
#
#     def recognize(self):
#         # 使用麦克风输入
#         p = pyaudio.PyAudio()
#         stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000,
#                         input=True, frames_per_buffer=8192)
#         stream.start_stream()
#
#         log.info("开始语音识别，请说话...")
#         # if self.speaker:
#         #     self.speaker.speak("开始语音识别，请说话...")
#
#         times = 0
#         result = []
#         while True:
#             if times >= 15:
#                 break
#
#             data = stream.read(4096)
#             if self.recognizer.AcceptWaveform(data):
#                 res = json.loads(self.recognizer.Result())
#                 # log.info(f"识别结果: {res}")
#                 result.append(res["text"])
#                 break
#             times += 1
#
#         return result
#
#
# class Speaker:
#     def __init__(self):
#         self.engine = pyttsx3.init()
#
#     @staticmethod
#     def remove_emoji(text):
#         emoji_pattern = re.compile(
#             "["
#             u"\U0001F600-\U0001F64F"  # 表情符号
#             u"\U0001F300-\U0001F5FF"  # 符号图案
#             u"\U0001F680-\U0001F6FF"  # 交通工具
#             u"\U0001F1E0-\U0001F1FF"  # 国旗
#             u"\U00002700-\U000027BF"  # 杂项符号
#             u"\U0001F900-\U0001F9FF"  # 补充表情
#             u"\U0001FA70-\U0001FAFF"  # 其他新增符号
#             u"\U00002500-\U00002BEF"  # 中文符号范围，不会删除
#             "]+", flags=re.UNICODE)
#         return emoji_pattern.sub('', text)
#
#     def speak(self, text):
#         text = self.remove_emoji(text)
#         self.engine.say(text)
#         self.engine.runAndWait()